package controllers

import javax.inject.Inject

import dao.couponDao
import models.Tables.CouponRow
import play.api.libs.json.Json
import play.api.mvc.{Action, Controller}

import scala.concurrent.Await
import scala.concurrent.duration.Duration

import scala.concurrent.ExecutionContext.Implicits.global

class BackgroundController @Inject()(coupondao:couponDao) extends Controller {


  /**
    * 数据库设计：
    * 卡券表
    * id:
    * 卡号：
    * 密码：
    * 使用状态：已使用/未使用
    * 创建日期
    * 到期时间：
    *
    */

  /**
    * 用户表
    * id:
    * 账号：
    * 密码：
    * 权限：管理员/员工
    * 使用人：
    * 手机号码：
    *
    */

  /**
    * 发货表：
    * 卡券编号：
    * 收货人：
    * 收货人手机号码：
    * 收获地址：
    * 收获时间：
    * 邮编：
    * 快递类型：
    * 快递编号：
    * 发货状态： 已发货/未发货
    *
    *
    */


  val seq = Seq("1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
    "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
    "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")


  def getCode: String = (1 to 25).map { x =>
    seq((math.random * 62).toInt)
  }.mkString

  def getPassword: String = (1 to 8).map { x =>
    seq((math.random * 62).toInt)
  }.mkString


  def addCode(nums:String,overDate:String) = Action{implicit request=>

    val row = (1 to nums.toInt).map{x=>
      CouponRow(getCode,getPassword,"0",utils.Utils.date2,utils.Utils.date2)
    }

    Await.result(coupondao.addCoupon(row),Duration.Inf)

    Ok(Json.obj("1" -> "1"))
  }

  def getAllCode = Action.async{implicit request=>

    coupondao.getAll.map{x=>
      val row = x.map{y=>
        Json.obj("code" -> y.code , "password" -> y.password, "state" -> y.state,"createdate" ->  y.createdate,
        "overdate" -> y.overdate)
      }
      Ok(Json.toJson(row))
    }
  }


  def backgroundHome = Action{implicit request=>
    Ok(views.html.background.homepage())
  }

}
